﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using PmsClient;

namespace web.ClientMgr
{
    public partial class Default : System.Web.UI.Page
    {
        /// <summary>
        /// 操作模式 i_Mode in ( "INSERT","UPDATE","")
        /// </summary>
        ///private String i_Mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Button_Save.Enabled = false;
                Button_Cancel.Enabled = false;

                /// <summary>
                /// 操作模式 i_Mode in ( "INSERT","UPDATE",null)
                /// </summary>
                Session["i_Mode"] = null;
                //HiddenField_Mode.Value = null;
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          //1  TextBox1.Text = GridView1.SelectedRow.Cells[3].Text;
          //2  TextBox1.Text = GridView1.SelectedDataKey[0].ToString();
          //TextBox1.Text = GridView1.SelectedValue.ToString();
            Session["i_Mode"] = null;
            DataSet idataset;
            try{
                idataset = DbHelperSQL.Query("SELECT * FROM ClientOfClient where clientid=" + int.Parse(GridView1.SelectedValue.ToString()) +" ");
            }catch (Exception ee)
            {
                LogService.Write("ClientMgr.Default查询客户信息失败！");
                LogService.Write(ee.Message);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('查询客户信息失败！')", true);
                return;
            }
            Session["ClientID"] = int.Parse(GridView1.SelectedValue.ToString());
            //HiddenField_Mode.Value = GridView1.SelectedValue.ToString();

            txtClientName.Text = idataset.Tables[0].Rows[0]["CLIENTNAME"].ToString();
            txtPersonSex.Text = idataset.Tables[0].Rows[0]["PERSONSEX"].ToString();
            txtPersonBirthday.Text = ((DateTime)idataset.Tables[0].Rows[0]["PERSONBIRTHDAY"]).ToString("yyyy-MM-dd");
            txtPersonNation.Text = idataset.Tables[0].Rows[0]["PERSONNATION"].ToString();
            txtPersonEduDegree.Text = idataset.Tables[0].Rows[0]["PERSONEDUDEGREE"].ToString();
            txtPersonMerriageState.Text = idataset.Tables[0].Rows[0]["PERSONMERRIAGESTATE"].ToString();
            txtPersonIDCardNO.Text = idataset.Tables[0].Rows[0]["PERSONIDCARDNO"].ToString();
            txtPersonCardActiveDate.Text = ((DateTime)idataset.Tables[0].Rows[0]["PERSONCARDACTIVEDATE"]).ToString("yyyy-MM-dd");
            txtPersonRegisterPlace.Text = idataset.Tables[0].Rows[0]["PERSONREGISTERPLACE"].ToString();
            txtPersonIDCardAddress.Text = idataset.Tables[0].Rows[0]["PERSONIDCARDADDRESS"].ToString();
            txtPersonOtherCardName.Text = idataset.Tables[0].Rows[0]["PERSONOTHERCARDNAME"].ToString();
            txtPersonOtherCardNo.Text = idataset.Tables[0].Rows[0]["PERSONOTHERCARDNO"].ToString();
            txtPersonCorpName.Text = idataset.Tables[0].Rows[0]["PERSONCORPNAME"].ToString();
            txtPersonProfession.Text = idataset.Tables[0].Rows[0]["PERSONPROFESSION"].ToString();
            txtClientCurTel.Text = idataset.Tables[0].Rows[0]["CLIENTCURTEL"].ToString();
            txtClientCurAddr.Text = idataset.Tables[0].Rows[0]["CLIENTCURADDR"].ToString();
            txtPersonIsHouseMaster.Text = idataset.Tables[0].Rows[0]["PERSONISHOUSEMASTER"].ToString();
            txtClientNO.Text = idataset.Tables[0].Rows[0]["CLIENTNO"].ToString();
            txtClientDesp.Text = idataset.Tables[0].Rows[0]["CLIENTDESP"].ToString();

        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            Session["i_Mode"] = "INSERT";
            //HiddenField_Mode.Value = null;
            PageControls.textbox_readonly_false(this.UpdatePanel_ClientInfo);
            PageControls.textbox_nvl(this.UpdatePanel_ClientInfo);
            txtPersonBirthday_CalendarExtender.Enabled = true;
            txtPersonCardActiveDate_CalendarExtender.Enabled = true;
            UpdatePanel_ClientInfo.Update();
            SetFocus(this.txtClientName);
            PageControls.button_enable_false(this.UpdatePanel_CMD);
            Button_Save.Enabled = true;
            Button_Cancel.Enabled = true;
        }

        protected void Button_Modify_Click(object sender, EventArgs e)
        {
            Session["i_Mode"] = "UPDATE";
            PageControls.textbox_readonly_false(this.UpdatePanel_ClientInfo);
            txtPersonBirthday_CalendarExtender.Enabled = true;
            txtPersonCardActiveDate_CalendarExtender.Enabled = true;
            UpdatePanel_ClientInfo.Update();
            SetFocus(this.txtClientName);
            PageControls.button_enable_false(this.UpdatePanel_CMD);
            Button_Save.Enabled = true;
            Button_Cancel.Enabled = true;
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string strErr = "";

            if (this.txtClientNO.Text == "")
            {
                strErr += "ClientNO不能为空！\\n";
            }
            if (this.txtClientName.Text == "")
            {
                strErr += "ClientName不能为空！\\n";
            }
            if (this.txtClientDesp.Text == "")
            {
                strErr += "ClientDesp不能为空！\\n";
            }
            if (this.txtClientCurTel.Text == "")
            {
                strErr += "ClientCurTel不能为空！\\n";
            }
            if (this.txtClientCurAddr.Text == "")
            {
                strErr += "ClientCurAddr不能为空！\\n";
            }
            if (this.txtPersonSex.Text == "")
            {
                strErr += "PersonSex不能为空！\\n";
            }
            if (this.txtPersonNation.Text == "")
            {
                strErr += "PersonNation不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtPersonBirthday.Text))
            {
                strErr += "PersonBirthday不是时间格式！\\n";
            }
            if (this.txtPersonEduDegree.Text == "")
            {
                strErr += "PersonEduDegree不能为空！\\n";
            }
            if (this.txtPersonMerriageState.Text == "")
            {
                strErr += "PersonMerriageState不能为空！\\n";
            }
            if (this.txtPersonIDCardNO.Text == "")
            {
                strErr += "PersonIDCardNO不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtPersonCardActiveDate.Text))
            {
                strErr += "PersonCardActiveDate不是时间格式！\\n";
            }
            if (this.txtPersonRegisterPlace.Text == "")
            {
                strErr += "PersonRegisterPlace不能为空！\\n";
            }
            if (this.txtPersonIDCardAddress.Text == "")
            {
                strErr += "PersonIDCardAddress不能为空！\\n";
            }
            if (this.txtPersonOtherCardName.Text == "")
            {
                strErr += "PersonOtherCardName不能为空！\\n";
            }
            if (this.txtPersonOtherCardNo.Text == "")
            {
                strErr += "PersonOtherCardNo不能为空！\\n";
            }
            if (this.txtPersonCorpName.Text == "")
            {
                strErr += "PersonCorpName不能为空！\\n";
            }
            if (this.txtPersonProfession.Text == "")
            {
                strErr += "PersonProfession不能为空！\\n";
            }
            if (this.txtPersonIsHouseMaster.Text == "")
            {
                strErr += "PersonIsHouseMaster不能为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(UpdatePanel_ClientInfo, strErr);
                //ScriptManager.RegisterStartupScript(UpdatePanel_ClientInfo, UpdatePanel_ClientInfo.GetType(), "", "alert('" + strErr + "!')", true);
                PageControls.textbox_readonly_false(this.UpdatePanel_ClientInfo);
                txtPersonBirthday_CalendarExtender.Enabled = true;
                txtPersonCardActiveDate_CalendarExtender.Enabled = true;
                UpdatePanel_ClientInfo.Update();
                SetFocus(this.txtClientName);
                PageControls.button_enable_false(this.UpdatePanel_CMD);
                Button_Save.Enabled = true;
                Button_Cancel.Enabled = true;
                return;
            }
            { 
            string ClientNO = this.txtClientNO.Text;
            string ClientName = this.txtClientName.Text;
            string ClientDesp = this.txtClientDesp.Text;
            string ClientCurTel = this.txtClientCurTel.Text;
            string ClientCurAddr = this.txtClientCurAddr.Text;
            string PersonSex = this.txtPersonSex.Text;
            string PersonNation = this.txtPersonNation.Text;
            DateTime PersonBirthday = DateTime.Parse(this.txtPersonBirthday.Text);
            string PersonEduDegree = this.txtPersonEduDegree.Text;
            string PersonMerriageState = this.txtPersonMerriageState.Text;
            string PersonIDCardNO = this.txtPersonIDCardNO.Text;
            DateTime PersonCardActiveDate = DateTime.Parse(this.txtPersonCardActiveDate.Text);
            string PersonRegisterPlace = this.txtPersonRegisterPlace.Text;
            string PersonIDCardAddress = this.txtPersonIDCardAddress.Text;
            string PersonOtherCardName = this.txtPersonOtherCardName.Text;
            string PersonOtherCardNo = this.txtPersonOtherCardNo.Text;
            string PersonCorpName = this.txtPersonCorpName.Text;
            string PersonProfession = this.txtPersonProfession.Text;
            string PersonIsHouseMaster = this.txtPersonIsHouseMaster.Text;

            PmsClient.Model.ClientOfClient model = new PmsClient.Model.ClientOfClient();

            model.ClientNO = ClientNO;
            model.ClientName = ClientName;
            model.ClientDesp = ClientDesp;
            model.ClientCurTel = ClientCurTel;
            model.ClientCurAddr = ClientCurAddr;
            model.PersonSex = PersonSex;
            model.PersonNation = PersonNation;
            model.PersonBirthday = PersonBirthday;
            model.PersonEduDegree = PersonEduDegree;
            model.PersonMerriageState = PersonMerriageState;
            model.PersonIDCardNO = PersonIDCardNO;
            model.PersonCardActiveDate = PersonCardActiveDate;
            model.PersonRegisterPlace = PersonRegisterPlace;
            model.PersonIDCardAddress = PersonIDCardAddress;
            model.PersonOtherCardName = PersonOtherCardName;
            model.PersonOtherCardNo = PersonOtherCardNo;
            model.PersonCorpName = PersonCorpName;
            model.PersonProfession = PersonProfession;
            model.PersonIsHouseMaster = PersonIsHouseMaster;

//            PmsClient.BLL.ClientOfClient bll = new PmsClient.BLL.ClientOfClient();
            String ii_Mode;
            ii_Mode = (String)Session["i_Mode"];
            switch (ii_Mode)
            {
                case "INSERT":
                    {
                        PmsClient.BLL.ClientOfClient bll_INSERT = new PmsClient.BLL.ClientOfClient();
                        bll_INSERT.Add(model); 
                        break; 
                    }
                case "UPDATE":
                    {
                        model.ClientID = (int)Session["ClientID"];
                        PmsClient.BLL.ClientOfClient bll_UPDATE = new PmsClient.BLL.ClientOfClient();
                        bll_UPDATE.Update(model); 
                        break; 
                    }
            }

            PageControls.textbox_readonly_true(this.UpdatePanel_ClientInfo);
            txtPersonBirthday_CalendarExtender.Enabled = false;
            txtPersonCardActiveDate_CalendarExtender.Enabled = false;
            UpdatePanel_ClientInfo.Update();
            PageControls.button_enable_true(this.UpdatePanel_CMD);
            Button_Save.Enabled = false;
            Button_Cancel.Enabled = false;
            }

        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            Session["i_Mode"] = "";
            PageControls.textbox_readonly_true(this.UpdatePanel_ClientInfo);
            txtPersonBirthday_CalendarExtender.Enabled = false;
            txtPersonCardActiveDate_CalendarExtender.Enabled = false;
            UpdatePanel_ClientInfo.Update();
            PageControls.button_enable_true(this.UpdatePanel_CMD);
            Button_Save.Enabled = false;
            Button_Cancel.Enabled = false;
        }

    }
}
